package cn.csg.iotplatform.module.system.controller.admin.user.vo.user;

import cn.csg.iotplatform.framework.excel.core.annotations.DictFormat;
import cn.csg.iotplatform.framework.excel.core.convert.DictConvert;
import cn.csg.iotplatform.module.common.annotations.Desensitization;
import cn.csg.iotplatform.module.common.enums.DesensitizationTypeEnum;
import cn.csg.iotplatform.module.system.enums.DictTypeConstants;
import cn.csg.iotplatform.module.system.util.ToolUtil;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

@Schema(description = "管理后台 - 用户信息 Response VO")
@Data
@ExcelIgnoreUnannotated
public class UserRespVO {

    @Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @ExcelProperty("用户编号")
    private Long id;

    @Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "iot")
    @ExcelProperty("用户账号")
    private String username;

    @Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
    @ExcelProperty("用户姓名")
    @Desensitization(type = DesensitizationTypeEnum.NICKNAME)
    private String nickname;

    @Schema(description = "部门ID", example = "我是一个用户")
    private Long deptId;

    @Schema(description = "部门名称", example = "IT 部")
    @ExcelProperty("所属部门")
    private String deptName;

    @Schema(description = "租户类型", example = "0")
    private Integer isTenant;

    @Schema(description = "工号", example = "1")
    @ExcelProperty("工号")
    @Desensitization(type = DesensitizationTypeEnum.USERNO)
    private String userNo;

    @Schema(description = "用户性别，参见 SexEnum 枚举类", example = "1")
    @ExcelProperty(value = "用户性别", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.USER_SEX)
    private Integer sex;

    @Schema(description = "岗位编号数组", example = "1")
    private Set<Long> postIds;

    @Schema(description = "手机号码", example = "15601691300")
    @ExcelProperty("手机")
    @Desensitization(type = DesensitizationTypeEnum.MOBILE)
    private String mobile;

    @Schema(description = "用户邮箱", example = "iot@iocoder.cn")
    @ExcelProperty("邮箱")
    @Desensitization(type = DesensitizationTypeEnum.EMAIL)
    private String email;

    @Schema(description = "用户头像", example = "https://www.iocoder.cn/xxx.png")
    @Desensitization(type = DesensitizationTypeEnum.AVATAR)
    private String avatar;

    @Schema(description = "状态，参见 CommonStatusEnum 枚举类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @ExcelProperty(value = "帐号状态", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.COMMON_STATUS)
    private Integer status;

    @Schema(description = "最后登录 IP", requiredMode = Schema.RequiredMode.REQUIRED, example = "192.168.1.1")
    @ExcelProperty("最后登录IP")
    private String loginIp;

    @Schema(description = "最后登录时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "时间戳格式")
    @ExcelProperty("最后登录时间")
    private LocalDateTime loginDate;

    @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "时间戳格式")
    private LocalDateTime createTime;

    @Schema(description = "失效日期", requiredMode = Schema.RequiredMode.REQUIRED, example = "时间戳格式")
    private LocalDateTime expirationDate;

    @Schema(description = "是否有效", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer isValid;

    @Schema(description = "禁用原因", example = "用户超过X天未登录系统自动禁用")
    private String disableReason;

    @Schema(description = "绑定ip地址", example = "127.0.0.1;127.0.0.1")
    private String userIp;

    @Schema(description = "借调部门ids", example = "[1,2,3]")
    private Set<Long> relationDeptIds;

    @Schema(description = "备注", example = "我是一个用户")
    @ExcelProperty("用户描述")
    private String remark;

    @Schema(description = "elink帐号", requiredMode = Schema.RequiredMode.REQUIRED, example = "elink帐号")
    @ExcelProperty("elink帐号")
    @Desensitization(type = DesensitizationTypeEnum.ELINK)
    private String elink;


    @ExcelProperty("租户类型")
    private String isTenantDesc;
    @ExcelProperty("是否有效")
    private String isValidDesc;
    @ExcelProperty("失效时间")
    private String expirationDateDesc;
    @ExcelProperty("创建时间")
    private String createTimeDesc;

    public String getIsTenantDesc() {
        if (ToolUtil.isEmpty(isTenant)) {
            return "";
        }
        switch (isTenant) {
            case 0:
                return "非租户";
            case 1:
                return "租户";
            default:
                return "";
        }
    }

    public String getIsValidDesc() {
        if (ToolUtil.isEmpty(isValid)) {
            return "";
        }
        switch (isValid) {
            case 0:
                return "禁用";
            case 1:
                return "启用";
            default:
                return "";
        }
    }

    public String getExpirationDateDesc() {
        if (ToolUtil.isEmpty(expirationDate)) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 创建格式化器
        return expirationDate.format(formatter).equals("1970-01-01 00:00:00") ? "" : expirationDate.format(formatter);
    }

    public String getCreateTimeDesc() {
        if (ToolUtil.isEmpty(createTime)) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 创建格式化器
        return createTime.format(formatter).equals("1970-01-01 00:00:00") ? "" : createTime.format(formatter);
    }
}
